<template>
  <div class="login-container">
    <el-card class="login-box">
      <h1 class="login-title">图书管理系统</h1>
      <div v-if="$route.query.message" class="message-tip">
        <el-alert :title="$route.query.message" type="warning" :closable="false"></el-alert>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        @submit.native.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            class="login-btn"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="bottom-links">
        <el-button type="text" @click="goToRegister">
          没有账号？立即注册
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.performLogin();
        }
      });
    },
    async performLogin() {
      try {
        const users = JSON.parse(localStorage.getItem("users")) || [];
        const user = users.find(u =>
            u.username === this.loginForm.username &&
            u.password === this.loginForm.password
        );

        if (user) {
          this.$store.dispatch('login', user);
          this.$message.success('登录成功！');

          // 处理重定向逻辑
          let redirect = '/home';
          if (this.$route.query.redirect) {
            redirect = this.$route.query.redirect;
          }

          await this.$router.push(redirect);
        } else {
          this.$message.error('用户名或密码错误');
        }
      } catch (error) {
        this.$message.error('登录失败，请重试');
      } finally {
        this.loading = false;
      }
    },
    goToRegister() {
      this.$router.push('/register');
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f4f4;
}

.login-box {
  width: 300px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.bottom-links {
  text-align: center;
  margin-top: 20px;
}

.message-tip {
  margin-bottom: 10px;
}
</style>